import VueRouter from "vue-router";
import Axios from "@/my_axios";

import loginVue from "@/components/loginVue.vue"
import registerVue from "@/components/registerVue.vue"
import indexVue from '@/components/indexVue.vue'

import homeVue from '@/components/homeVue.vue'
import geologyWKVue from '@/components/geologyWKVue.vue'
import soilWKVue from '@/components/soilWKVue.vue'
import soilAnalyseVue from '@/components/soilAnalyseVue.vue'

import userVue from '@/components/userVue.vue'
import testVue from '@/components/testVue'

// import Store from "@/store";

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            name:'test',
            path:'/test',
            component: testVue,
            // component:()=>{
            //     import('@/components/indexVue2.vue');
            // }
        },
        {
            path:'/',
            redirect: '/login',
        },
        {
            name:'login',
            path:'/login',
            component:loginVue
        },
        {
            name:'register',
            path:'/register',
            component:registerVue
        },
        {
            name:'index',
            path:'/index',
            component:indexVue,
            children:[
                {
                    name:'home',
                    path:'home',
                    component:()=>import('@/components/layoutBridge.vue'),
                    meta: {title:"首页"}, // 功能页面需要身份验证
                    redirect:'/index/home/overview',
                    children:[
                        {
                            name:'overview',
                            path:'overview',
                            component:homeVue,
                            meta: {title:"样本点总览", requiresAuth: true}, // 功能页面需要身份验证
                        },
                    ]
                },
                {
                    name:'geology',
                    path:'geology',
                    component:()=>import('@/components/layoutBridge.vue'),
                    redirect:'/index/geology/geologywk',
                    meta: {title:"地质专题"},
                    children:[
                        {
                            name:'geologyWK',
                            path:'geologywk',
                            component: geologyWKVue,
                            meta: {title:"地质采样点", requiresAuth: true}, // 功能页面需要身份验证
                        },
                    ]
                },
                {
                    name:'soil',
                    path:'soil',
                    component:()=>import('@/components/layoutBridge.vue'),
                    redirect:'/index/soil/soilwk',
                    meta: {title:"土壤专题"},
                    children:[
                        {
                            name:'soilWK',
                            path:'soilwk',
                            component: soilWKVue,
                            meta: {title:"土壤采样点", requiresAuth: true}, // 功能页面需要身份验证
                        },
                        {
                            name:'soilAnalyse',
                            path:'analyse',
                            component: soilAnalyseVue,
                            meta: {title:"土壤类型", requiresAuth: true}, // 功能页面需要身份验证
                        },
                    ]
                },
                {
                    name:'zoology',
                    path:'zoology',
                    component:()=>import('@/components/layoutBridge.vue'),
                    redirect:'/index/zoology/zoologywk',
                    meta: {title:"生物专题"},
                    children:[
                        {
                            name:'zoologyWK',
                            path:'zoologywk',
                            component: ()=> import('@/components/zoologyWKVue.vue'),
                            meta: {title:"生物调查点", requiresAuth: true}, // 功能页面需要身份验证
                        },
                    ]
                },
                {
                    name:'user',
                    path:'user',
                    component:()=>import('@/components/layoutBridge.vue'),
                    meta: {title:"用户管理"},
                    redirect: '/index/user/total',
                    children:[
                        {
                            name:'total',
                            path:'total',
                            component:userVue,
                            meta: {title:"系统概览", requiresAuth: true}, // 功能页面需要身份验证
                        },
                        {
                            name:'userApply',
                            path:'userApply',
                            component:()=> import('@/components/userApply.vue'),
                            meta: {title:"申请处理", requiresAuth: true}, // 功能页面需要身份验证
                        }
                    ]
                },
            ]
        },
    ]
})

router.beforeEach((to, from, next)=> {
    // var data = {
    //     name: Store.state.name,
    // }
    // // 阻止重复重定向// 这种js方式解决目前一点都不好，如果是需要界面上点击发生路由跳转，强烈建议用 "routerTo包裹"的方式
    // if(to.fullPath == from.fullPath){
    //     next(false);
    //     // console.log('to.path:', to.path, 'from.path:',from.path)
    // }else {
    //     next();
    // }


    Axios.get('/session')
    .then((resp)=>{
        var resp_errno = resp.data.errno;
        console.log("suc：", resp, resp_errno)
        if (to.meta.requiresAuth && resp_errno==4101) {
            next('/login');
        } else {
            next();
        }
    }).catch((err)=>{
        console.log("服务器用户验证接口fail:", err)
        next('/login');
    })


})

export default router